package com.iwjw.ibatis.session;

import com.iwjw.ibatis.binding.MybatisMapperRegistry;
import com.iwjw.ibatis.mapper.AutoSqlInjector;
import com.iwjw.ibatis.mapper.ISqlInjector;
import org.apache.ibatis.binding.MapperRegistry;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;

import java.util.Set;
import java.util.concurrent.ConcurrentSkipListSet;

/**
 * 主要继承Configuration默认把涉及Mapper注册器的方法全部重写
 *
 * @author weiwei.huang
 * @Time 2016/10/14
 */
public class MybatisConfiguration extends Configuration {

    public final static Set<String> MAPPER_REGISTRY_CACHE = new ConcurrentSkipListSet();
    public final static Set<String> RESULTMAP_REGISTRY_CACHE = new ConcurrentSkipListSet();
    /*
     * SQL 注入器，实现 ISqlInjector 或继承 AutoSqlInjector 自定义方法
     */
    public static ISqlInjector SQL_INJECTOR = new AutoSqlInjector();
    public final MybatisMapperRegistry mybatisMapperRegistry = new MybatisMapperRegistry(this);

    @Override
    public void addMappedStatement(MappedStatement ms) {
        if (this.mappedStatements.containsKey(ms.getId())) {
            return;
        }
        super.addMappedStatement(ms);
    }

    @Override
    public MapperRegistry getMapperRegistry() {
        return this.mybatisMapperRegistry;
    }

    @Override
    public void addMappers(String packageName, Class<?> superType) {
        this.mybatisMapperRegistry.addMappers(packageName, superType);
    }

    @Override
    public void addMappers(String packageName) {
        this.mybatisMapperRegistry.addMappers(packageName);
    }

    @Override
    public <T> void addMapper(Class<T> type) {
        this.mybatisMapperRegistry.addMapper(type);
    }

    @Override
    public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
        return this.mybatisMapperRegistry.getMapper(type, sqlSession);
    }


    @Override
    public boolean hasMapper(Class<?> type) {
        return this.mybatisMapperRegistry.hasMapper(type);
    }
}
